local skel = fk.CreateSkill{
  name = "tea__dengjie",
}
Fk:loadTranslationTable{
  ["tea__dengjie"] = "登阶",
  [":tea__dengjie"] = "出牌阶段限一次，你可重铸装备区里所有牌，然后重铸更多张锦囊牌，最后重铸更多张基本牌。当你以此法重铸牌后，弃置手牌数大于你的角色一张牌，若没有手牌数大于你的角色，你摸一张牌。",
  ["#tea__dengjie-trick"] = "登阶：重铸至少 %arg 张锦囊牌",
  ["#tea__dengjie-basic"] = "登阶：重铸至少 %arg 张基本牌",
  ["#tea__dengjie-choose"] = "登阶：弃置一名角色一张牌",
  ["#tea__dengjie_discard"] = "登阶：选择一张牌弃置",
  ["#tea__dengjie-invoke"] = "登阶：重铸装备区的所有牌",
}
---@class function
---@param room Room
---@param player ServerPlayer
local doDengjie = function (room, player)
  local targets = table.filter(room:getOtherPlayers(player), function (p)
    return #p:getCardIds("h") > #player:getCardIds("h")
  end)
  if #targets > 0 then
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = false,
      prompt = "#tea__dengjie-choose",
    })
    if #to == 1 then
      to = to[1]
      local cid = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = skel.name,
        prompt = "#tea__dengjie_discard"
      })
      if cid then
        room:throwCard(cid, skel.name, to, player)
      end
    end
  else
    player:drawCards(1, skel.name)
  end
end
---get the voice index
---@param player ServerPlayer
---@param tab integer[]
---@return integer
local getVoice = function (player, tab)
  local isZhaozhong = (player.room:getBanner("RoundCount") or 1) % 2 + 1
  return tab[isZhaozhong]
end
---@type ActiveSkillSpec
local dengjie = {
  mute = true,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  prompt = "#tea__dengjie-invoke",
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0 and #player:getCardIds("e") > 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    room:notifySkillInvoked(player, skel.name, "drawcard")
    player:broadcastSkillInvoke(skel.name, getVoice(player, { 1, 4 }))
    local r_equip = room:recastCard(player:getCardIds("e"), player, skel.name)
    if player.dead then return end
    doDengjie(room, player)
    if player.dead then return end
    local trick = table.filter(player:getCardIds("h"), function(id)
      return Fk:getCardById(id).type == Card.TypeTrick
    end)
    if #trick > #r_equip then
      local _trick = room:askToCards(player, {
        include_equip = false,
        min_num = #r_equip + 1,
        max_num = 999,
        cancelable = false,
        skill_name = skel.name,
        prompt = "#tea__dengjie-trick:::" .. tostring(#r_equip + 1),
        pattern = ".|.|.|.|.|trick"
      })

      if #_trick > #r_equip then
        room:notifySkillInvoked(player, skel.name, "drawcard")
        player:broadcastSkillInvoke(skel.name, getVoice(player, { 2, 5 }))
        local r_trick = room:recastCard(_trick, player, skel.name)
        if player.dead then return end
        doDengjie(room, player)
        if player.dead then return end
        local basic = table.filter(player:getCardIds("h"), function(id)
          return Fk:getCardById(id).type == Card.TypeBasic
        end)
        if #basic > #r_trick then
          local _basic = room:askToCards(player, {
            include_equip = false,
            min_num = #r_trick + 1,
            max_num = 999,
            cancelable = false,
            skill_name = skel.name,
            prompt = "#tea__dengjie-basic:::" .. tostring(#r_trick + 1),
            pattern = ".|.|.|.|.|basic"
          })
          if #_basic > #r_trick then
            room:notifySkillInvoked(player, skel.name, "drawcard")
            player:broadcastSkillInvoke(skel.name, getVoice(player, { 3, 6 }))
            room:recastCard(_basic, player, skel.name)
            if player.dead then return end
            doDengjie(room, player)
          end
        end
      end
    end
  end
}
skel:addEffect("active", dengjie)
return skel